	
/*
	缓存记录：
		sessionStorage.loginStat 		缓存登陆状态
		sessionStorage.indexButtonGroup_tabType 	缓存当前选择分类
		sessionStorage.randSum = '0';		当天浏览的总条数 
		sessionStorage.Current_TableType	随机浏览时当前选择的分类
		sessionStorage.Current_Data_Index	随机浏览时当前选择的数据下标

		localStorage.info_all 		缓存全部数据信息
		localStorage.tableType		缓存分类列表
		localStorage.Random_TableType	缓存随机浏览时对有数据的表下标列表

		'localStorage.Data_Index_List_'+Current_TableType	随机浏览时当前选择列的数据下标列表缓存
*/








	var info_all;
	var random_count=0;	//随机点击次数累计，分类切换时归零




	//清除指定或全部的sess缓存
	function ClearSessCache(type){
		//清除指定sess缓存
		if(type){
			var clear_str1 = "sessionStorage.removeItem('"+type+"')";
			var clear_str2 = "localStorage.removeItem('"+type+"')";
			eval(clear_str1);
			eval(clear_str2);
		}else{	//清除全部sess缓存
			localStorage.clear();
			sessionStorage.clear();
		}
	}



	//解析分类列表数据
	function SetSelect(){
		var res = localStorage.tableType;
		var option_tmp = "<select id='indexButtonGroup_select'><option value='all' selected>全部</option>";
		//console.log(res);
		if(!/^\[.*\]$/.test(res)){
			alert('加载分类异常!');return;
		}
		var tabType = JSON.parse(res);
		tabType.forEach(function(v,i,t){
			option_tmp += "<option value='"+v[0]+"'>"+ChangeChar(v[1],1)+"</option>";
		});
		option_tmp += "</select>";
		$('#indexButtonGroup_tabType').html(option_tmp);
	}

	//获取并解析分类列表数据
	function Get_TypeList(){
		$.ajax({
			url:'./Runtime/Cache_long/indexButtonGroup_tabType?r='+Math.random(),
			async:false,
			success:function(res){
				//$('#indexButtonGroup_tabType').html(res);
				localStorage.tableType = res;
				SetSelect();
			},
			error:function(){
				$.ajax({
					url:'./Control/indexButtonGroup_tabType.php',
					async:false,
					success:function(res){
						localStorage.tableType = res;
						SetSelect();
					}
				});
			}
		});
	}


	//获取全部信息数据(local -> cache -> after)
	function Get_Info_All(fun){
		if(!localStorage.info_all || !/^{.*}$/.test(localStorage.info_all)){
			$.ajax({
				url:'./Runtime/CacheInfoAll/info_all?r='+Math.random(),
				success:function(res){
					console.log('cache_get');
					localStorage.info_all = res;
					fun(localStorage.info_all);
				},
				error:function(){
					$.ajax({
						url:'./Control/indexButtonGroup_infoAll.php',
						type:'post',
						success:function(res){
							console.log('after_get');
							localStorage.info_all = res;
							//format_infoAll(res)
							fun(localStorage.info_all);
						},
						error:function(){
							alert('ajax is error!');
						}
					});
				}
			});
		}else{
			console.log('local_get');
			fun(localStorage.info_all);
		}
	}



	//获取分类下标列表(只含有数据的分类)
	function Get_Random_TableType(){
		if(!localStorage.Random_TableType){
			Get_Info_All(function(res){
				if(!/^{.*}$/.test(res)){
					console.log('没有数据!');
					return;
				}
				var data_all = JSON.parse(res);
				var Random_TableType = new Array();
				for(var i in data_all){
					Random_TableType.push(i);	//保存一级下标
				}
				localStorage.Random_TableType = JSON.stringify(Random_TableType);
				return localStorage.Random_TableType;
			});
		}
		return localStorage.Random_TableType;
	}






$(function(){
	if(!/^\[.*\]$/.test(localStorage.tableType)){
		Get_TypeList();
	}else{
		SetSelect();
	}






	if(sessionStorage.loginStat == 'true'){
		$('#login').removeClass('displayBlockInline').addClass('displayNone');
		$('#exit').removeClass('displayNone').addClass('displayBlockInline');
	}else{

		$('#indexButtonGroup_infoAdd').hide();
		$('#indexButtonGroup_infoDel').hide();
		$('#indexButtonGroup_tabTypeAdd').hide();
		$('#indexButtonGroup_tabTypeDel').hide();
		//$('#indexButtonGroup_clearCache').hide();
		$('#indexButtonGroup_joinAll').hide();
		$('#login').removeClass('displayNone').addClass('displayBlockInline');
		$('#exit').removeClass('displayBlockInline').addClass('displayNone');
	}

	$('#login').click(function(){
		location.href='login.html';
	});
	$('#exit').click(function(){
		sessionStorage.loginStat = false;
		location.reload();
	});




	//清除缓存
	$('#indexButtonGroup_clearCache').click(function(){
		if(!confirm('确认清除缓存?')){
			return;
		}
		if(123 != prompt('输入密码')){
			alert('操作码不正确!');
			return;
		}
		localStorage.clear();
		sessionStorage.clear();
		$.ajax({
			url:'./Control/clearCache.php',
			success:function(res){
				alert('缓存以清除成功！');
			}
		});
	});



	//缓存当前选择的类别，并维持状态
	if(sessionStorage.indexButtonGroup_tabType){
		$('#indexButtonGroup_select').val(sessionStorage.indexButtonGroup_tabType);
	}else{
		$('#indexButtonGroup_select').val('all');
		sessionStorage.indexButtonGroup_tabType = $('#indexButtonGroup_select').find('option:selected').val();
	}
	$('#indexButtonGroup_select').change(function(){
		random_count = 0;
		sessionStorage.randSum = '0';
		$('#index_right,#index_left').val('');
		sessionStorage.indexButtonGroup_tabType = $('#indexButtonGroup_select').find('option:selected').val();
	});


	//总览
	$('#indexButtonGroup_infoAll').click(function(){
		if(!confirm('确认浏览全部内容?')){
			return;
		}
		var tid = $('#indexButtonGroup_select').find('option:selected').val();
	//	var tName = $('#indexButtonGroup_select').find('option:selected').text().trim();
	//	console.log(tid)

		var view_str = '';	
		if(!info_all){
			//获取全部数据
			Get_Info_All(function(res){
				if(/^{.*}$/.test(res)){
					info_all = JSON.parse(res);
					if(tid == 'all'){
						for(var j in info_all){
							if(info_all[j]){
								for(var i=0;i<info_all[j].length;i++){
									view_str += info_all[j][i][0]+"\r\n";
								}
							}
						}
					}else{
						if(info_all[tid]){
							for(var i=0;i<info_all[tid].length;i++){
								view_str += info_all[tid][i][0]+"\r\n";
							}
						}
					}
				}
				//$("#index_left").val(ChangeChar(view_str,1));
			});
		}else{
			console.log('variable_get');
			var view_str = '';	
			if(tid == 'all'){
				for(var j in info_all){
					if(info_all[j]){
						for(var i=0;i<info_all[j].length;i++){
							view_str += info_all[j][i][0]+"\r\n";
						}
					}
				}
			}else{
				if(info_all[tid]){
					for(var i=0;i<info_all[tid].length;i++){
						view_str += info_all[tid][i][0]+"\r\n";
					}
				}
			}
			$("#index_left").val(view_str);
		}
		view_str = !view_str?'暂无数据':ChangeChar(view_str,1);
		$("#index_left").val(ChangeChar(view_str,1));
	});






	var random_click_time;	//预防短时内连点时间保存变量

	//随机浏览
	$('#indexButtonGroup_infoRandom').click(function(){
		//预防短时内连点
		if(!random_click_time){
			random_click_time = new Date().getTime();
		}else{
			var old_time = random_click_time + 500;
			if(old_time > new Date().getTime()){
				return;
			}else{
				random_click_time = new Date().getTime();
			}
		}
		random_count += 1;

		//一：选择分类表的下标
		var TableType_List = Get_Random_TableType();	//获取有数据的表下标列表
		if(!TableType_List || TableType_List.length < 1){
			console.log('没有数据2');
			return;
		}
		TableType_List = JSON.parse(TableType_List);
		//console.log(TableType_List);
		var tid = $('#indexButtonGroup_tabType').find('option:selected').val();
		//获取当前选择的分类表下标
		if(tid == 'all'){
			//随机选取一个分类
			var Current_TableType = TableType_List[Math.floor(Math.random() * TableType_List.length)];
		}else{
			//选择指定的分类
			var Current_TableType = tid;
		}


		//二：根据表下标，查询或缓存该表下数据的下标缓存
		var Data_Index_List = 'localStorage.Data_Index_List_'+Current_TableType;
		//console.log(Data_Index_List);return;
		if(!eval(Data_Index_List)){	//如果指定表的数据下标列表缓存不存在就获取并缓存
			//获取指定分类下的数据下标列表，并缓存
			Get_Info_All(function(info_all){
				info_all = JSON.parse(info_all);
				var Index_List_Arr = new Array();
				for(var i=0;i<info_all[Current_TableType].length;i++){
					Index_List_Arr.push(i);
				}
				Index_List_Str = JSON.stringify(Index_List_Arr);
				var Data_Index_List_Save = Data_Index_List+"='"+Index_List_Str+"'";
				eval(Data_Index_List_Save);
			});
		}


		//三：随机选择分类的数据列表下标
		var Data_Index_List_arr = JSON.parse(eval(Data_Index_List));	//将缓存的下标列表字串转换成数组
		//console.log(Data_Index_List_arr)
		
		var Random_Index = Math.floor(Math.random()*Data_Index_List_arr.length);	//随机获取数组中的一个下标
		
		var Current_Data_Index = Data_Index_List_arr[Random_Index];	//*获取随机下标对应的值
		//console.log(Current_Data_Index)
		
		Data_Index_List_arr.splice(Random_Index,1);	//删除该随机下标的元素
		if(Data_Index_List_arr.length < 1){	//判断是置空还是保存缓存
			var Data_Index_List_Save = Data_Index_List+"=''";
		}else{
			var Data_Index_List_Save = Data_Index_List+"='"+JSON.stringify(Data_Index_List_arr)+"'";
		}

		//再将去除选择元素后的数组进行重新缓存
		eval(Data_Index_List_Save);

		//四：最后显示随机选择分类下数据的信息
		Get_Info_All(function(info_all){
			var info_all = JSON.parse(info_all);
			$('#index_left').val(ChangeChar(info_all[Current_TableType][Current_Data_Index][0],1));
		});

		$('#index_right').val('当前分类以浏览了'+random_count+'次');
	});



	//模糊查询
	$('#indexButtonGroup_infosQuery,#indexButtonGroup_infosQuery2').click(function(){
		$('#index_right').val('');

		var left = $('#index_left').val().trim();
		if(!left){
			return;
		}
		var type = $(this).attr('type');
		if(type=='title'){
			var type_val = 0;
		}else{
			var type_val = 1;
		}
		var tid = $('#indexButtonGroup_select').find('option:selected').val();

		var right = '';
		Get_Info_All(function(info_all){
			if(/^{.*}$/.test(info_all)){
				info_all = JSON.parse(info_all);
				if(tid == 'all'){
					for(var j in info_all){
						for(var i=0;i<info_all[j].length;i++){
							if(ChangeChar(info_all[j][i][type_val],1).match(left)){
								right += info_all[j][i][0]+"　-->\r\n"+info_all[j][i][1]+"\r\n\r\n\r\n";
							}
						}
					}
				}else{
					for(var i=0;i<info_all[tid].length;i++){
						if(ChangeChar(info_all[tid][i][type_val],1).match(left)){
							right += info_all[tid][i][0]+"　-->\r\n"+info_all[tid][i][1]+"\r\n\r\n\r\n";
						}
					}
				}
			}
			right = !right?'暂无数据':right;
			$('#index_right').val(ChangeChar(right,1));
		});
	});








	//查询
	$('#indexButtonGroup_infoQuery').click(function(){
		var left = ChangeChar($('#index_left').val().trim());
		if(!left){
			return;
		}
		var tid = $("#indexButtonGroup_select").find('option:selected').val().trim();
		Get_Info_All(function(info_all){
			if(!/^{.*}$/.test(info_all)){
				return;
			}
			var info_all = JSON.parse(info_all);
			var right = '';

			if(tid == 'all'){
				for(var j in info_all){
					for(var i=0;i<info_all[j].length;i++){
						if(left == info_all[j][i][0]){
							right += info_all[j][i][1]+"\r\n\r\n\r\n";
						}
					}
				}
			}else{
				for(var i=0;i<info_all[tid].length;i++){
					if(left == info_all[tid][i][0]){
						right = info_all[tid][i][1];
					}
				}
			}
			
			right = !right?'暂无数据!':right;
			$('#index_right').val(ChangeChar(right,1));
		});
	});








	//添加类别
	$('#indexButtonGroup_tabTypeAdd').click(function(){
		var tabName = ChangeChar($('#index_left').val().trim());
		if(tabName.length <1 || tabName.length > 30){
			alert('类别名不能为空或过长！');
			return;
		}
		if(!confirm('确认添加新类别：'+$('#index_left').val().trim()+'?')){
			return;
		}
		setToken('add_tabtype',function(t){
			$.ajax({
				url:"./Control/indexButtonGroup_tabTypeAdd.php",
				data:{
					tabName:tabName,
					token:t
				},
				type:'post',
				success:function(res){
					if(res == 'ok'){
						//localStorage.tableType = '';
						ClearSessCache('tableType');	//清除分类列表缓存
						ClearSessCache('info_all');		//清除全部数据缓存
						ClearSessCache('Random_TableType');	//清除随机时的有数据的表下标列表缓存

						alert('添加成功！');
						location.reload(1);
					}else{
						alert(res);
					}
				}
			});
		});
	});

	//删除类别
	$('#indexButtonGroup_tabTypeDel').click(function(){
		var tName = $("#indexButtonGroup_select").find('option:selected').text().trim();
		var tid = $("#indexButtonGroup_select").find('option:selected').val().trim();
		if( tName == '全部'){
			alert('请选择类别!');
			return;
		}
		if(!confirm('确认删除"'+tName+'"及其数据？不可恢复！')){
			return;
		}
		if(123 != prompt('输入密码')){
			alert('操作码不正确!');
			return;
		}
		setToken('tabTypeDel',function(t){
			$.ajax({
				url:'./Control/indexButtonGroup_tabTypeDel.php',
				type:'post',
				data:{
					token:t,
					tid:tid,
					tName:tName
				},
				success:function(res){
					if(res != 'ok'){
						alert('操作异常!');return;
					}
					ClearSessCache('tableType');	//清除分类列表缓存
					ClearSessCache('info_all');		//清除全部数据缓存
					ClearSessCache('indexButtonGroup_tabType');		//清除全部数据缓存
					ClearSessCache('Random_TableType');	//清除随机时的有数据的表下标列表缓存
					location.reload(1);
				}
			});
		});

	});




	//添加信息
	$('#indexButtonGroup_infoAdd').click(function(){
		var tid = $('#indexButtonGroup_select').find('option:selected').val();
		var tName = $('#indexButtonGroup_select').find('option:selected').text().trim();
		if(tid == 'all'){alert('请选择类别！');return;}
		var data = ChangeChar($('#index_left').val().trim());

		var data2 = data.split('---|');
		if(!data2[0] || !data2[1] ){
			alert('添加数据格式为: a ---| aaa');
			return;
		}
		setToken('infoAdd',function(t){
			var left = data2[0].trim();
			var right = data2[1].trim();
			$.ajax({
				url:'./Control/indexButtonGroup_infoAdd.php',
				data:{
					left:left,
					right:right,
					tid:tid,
					tName:tName,
					token:t
				},
				type:'post',
				success:function(res){
					if(res == 'ok'){
						info_all = null;
						ClearSessCache('info_all');
						ClearSessCache('Random_TableType');	//清除随机时的有数据的表下标列表缓存
						toast('添加成功!',2000,1);
					}
				},
				error:function(){
					toast('ajax is error',1000,1);
				}
			});
		});

	});


	//数据删除
	$('#indexButtonGroup_infoDel').click(function(){
		var tmpVal = ChangeChar($('#index_left').val().trim());
		if(!tmpVal){
			return;
		}
		var tid = $('#indexButtonGroup_select').find('option:selected').val();
		var tName = $('#indexButtonGroup_select').find('option:selected').text().trim();
		if(tid == 'all'){
			alert('全部状态下不可删除！');return;
		}
		if(!tmpVal){
			alert('请输入要删除的信息！');return;
		}
		if(!confirm('确认删除数据"'+tmpVal+'"?')){
			return;
		}
		if(123 != prompt('输入密码')){
			alert('操作码不正确!');
			return;
		}
		setToken('infoDel',function(t){
			$.ajax({
				url:'./Control/indexButtonGroup_infoDel.php',
				data:{
					token:t,
					left:tmpVal,
					tid:tid,
					tName:tName
				},
				type:'post',
				success:function(res){
					if(res == 'ok'){
						info_all = null;
						ClearSessCache('info_all');
						ClearSessCache('Random_TableType');	//清除随机时的有数据的表下标列表缓存
						alert('删除成功!');
					}else{
						alert('删除失败!');
					}
				},
				error:function(){
					alert('ajax is error!');
				}
			});
			
		});
	});


	//是否参与All浏览
	$('#indexButtonGroup_joinAll').click(function(){
		if(!confirm('确认要操作?')){
			return;
		}
		var selectVal = $('#index_button_groups').find('option:selected').val();
		if(selectVal == 'all'){
			alert('全部选项时不可用！');return;
		}
		$.ajax({
			url:'./Control/indexButtonGroup_joinAll.php',
			data:{
				tid:selectVal
			},
			type:'post',
			success:function(res){
				alert(res);
				location.reload(1);
			}
		});
	});
});




/*
	### 检测并替换字串中的特殊字符为实体字符 (' , " , > , < , / , \)
	### 参一：源字串 
	### 参二：是否反解析，默认空为特殊字符转实体
	### return : 返回替换后的字串
	### 示例：
		var content = $('textarea[name=content]').val().trim();
		content = ChangeChar(content);
		或
		ChangeChar(obj.info_title,1)
*/
function ChangeChar(val,stat){
	if(stat){
		var p1 = /&#60/g;
		val = val.replace(p1,"<");
		var p2 = /&#62/g;
		val = val.replace(p2,">");
		var p2 = /&#39/g;
		val = val.replace(p2,"'");
		var p2 = /&#34/g;
		val = val.replace(p2,'"');
		var p2 = /&#47/g;
		val = val.replace(p2,"\/");
		var p2 = /&#92/g;
		val = val.replace(p2,"\\");

		var p1 = /&amp;#60/g;
		val = val.replace(p1,"<");
		var p2 = /&amp;#62/g;
		val = val.replace(p2,">");
		var p2 = /&amp;#39/g;
		val = val.replace(p2,"'");
		var p2 = /&amp;#34/g;
		val = val.replace(p2,'"');
		var p2 = /&amp;#47/g;
		val = val.replace(p2,"\/");
		var p2 = /&amp;#92/g;
		val = val.replace(p2,"\\");
		return val;
	}else{
		var p1 = /</g;
		val = val.replace(p1,"&#60");
		var p2 = />/g;
		val = val.replace(p2,"&#62");
		var p2 = /'/g;
		val = val.replace(p2,"&#39");
		var p2 = /"/g;
		val = val.replace(p2,"&#34");
		var p2 = /\//g;
		val = val.replace(p2,"&#47");
		var p2 = /\\/g;
		val = val.replace(p2,"&#92");
		return val;
	}
}
